<template>
  <div>
    <el-dialog :title="title"
      :visible.sync="dialogVisible"
      width="50%"
      top="5vh"
      @close.native="cancel"
      :close-on-click-modal="false">
      <el-form ref="ruleForm"
        :model="form"
        :rules="rules"
        label-width="160px">
        <el-form-item label="军队使用单位："
          prop="armyCompany">
          <el-input v-model="form.armyCompany"
            placeholder="请输入"
            :disabled="isLook=='look'" />
        </el-form-item>
        <el-form-item label="共享资源名称："
          prop="title">
          <el-input v-model="form.title"
            placeholder="请输入"
            :disabled="isLook=='look'" />
        </el-form-item>
        <el-form-item label="资源管理单位："
          prop="companyName">
          <el-input v-model="form.companyName"
            placeholder="请输入"
            :disabled="isLook=='look'" />
        </el-form-item>
        <el-form-item label="使用起始时间："
          prop="startTime">
          <el-date-picker style="width:100%"
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            :disabled="isLook=='look'">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="使用结束时间："
          prop="endTime">
          <el-date-picker style="width:100%"
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            :disabled="isLook=='look'">
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="content">
          <template slot="label">
            <span>共享使用情况</span>
            <el-tooltip class="item"
              effect="dark"
              content="对使用地方共享资源的服务情况进行简要描述"
              placement="top">
              <i class="el-icon-question"
                style="color:#2c68ff" />
            </el-tooltip>
            <span>:</span>
          </template>
          <el-input v-model="form.content"
            placeholder="请输入"
            type="textarea"
            :rows="4"
            :disabled="isLook=='look'" />
        </el-form-item>
        <el-form-item label="使用评价："
          prop="replyContent">
          <el-rate v-model="form.evaluate"
            show-text
            :texts="['极差', '不满意', '一般', '基本满意', '非常满意']"
            :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
            style="padding-top: 10px;">
          </el-rate>
        </el-form-item>
      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          @click="submitForm"
          v-if="isLook!=='look'">确 定</el-button>
        <el-button @click="
          cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { uploadFile } from '@/api/common.js'
import {
  insertArticle,
  editArticle
  // articleDetail
} from '@/api/publishManage.js'
export default {
  data() {
    return {
      dialogVisible: false,
      iconClasses: ['icon-rate-face-1', 'icon-rate-face-2', 'icon-rate-face-3'],
      isLook: '',
      accept: '*',
      title: '',
      form: {
        evaluate: null,
        title: '',
        type: '',
        name: '',
        createdTime: '',
        fileId: null,
        fileName: null
      },
      facilityType: ['政策法规', '管理制度'],
      file: null,
      rules: {
        title: [
          { required: true, message: '文件名称不能为空！', trigger: 'blur' }
        ],
        type: [{ required: true, message: '来源不能为空！', trigger: 'blur' }],
        content: [
          { required: true, message: '内容不能为空！', trigger: 'blur' }
        ],
        fileName: [{ required: true, message: '请上传', trigger: 'blur' }]
      }
    }
  },
  created() {},
  methods: {
    show(obj) {
      this.dialogVisible = true
      this.title = obj.title
      this.isLook = obj.isLook
      if (obj.data) {
        this.form = Object.assign(obj.data)
        // articleDetail({ articleId: obj.data.articleId }).then(res => {
        //   this.form = res.data
        // })
      }
    },
    submitForm() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          if (this.file) {
            const formData = new FormData()
            formData.append('file', this.file)
            formData.append('pathType', this.file.name.split('.').pop())
            uploadFile(formData).then(res => {
              this.form.fileId = res.data.fileId
              if (this.form.articleId) {
                editArticle(this.form).then(() => {
                  this.cancel()
                })
              } else {
                insertArticle(this.form).then(() => {
                  this.cancel()
                })
              }
            })
          } else {
            if (this.form.articleId) {
              editArticle(this.form).then(() => {
                this.cancel()
              })
            } else {
              insertArticle(this.form).then(() => {
                this.cancel()
              })
            }
          }
        } else {
          return false
        }
      })
    },
    cancel() {
      this.form = this.$options.data().form
      this.$refs.ruleForm.clearValidate()
      this.dialogVisible = false
      this.isLook = false
      this.file = null
      this.$emit('close')
    },
    handleChange(file) {
      this.file = file.raw
      this.form.fileName = file.name
    },
    // 多次上传覆盖
    handleExceed(file) {
      console.log('[ file ] >', file)
      this.$refs.uploadRef.clearFiles()
      // this.handleChange(file[0])
      this.file = file[0]
      this.form.fileName = file[0].name
    },
    downloadLink(item) {
      this.downloadByUrl(item.fileName, item.filePath)
    },
    closeFile() {
      this.file = null
      this.$refs.uploadRef.clearFiles()
    }
  }
}
</script>

<style lang="less" scoped>
:deep(.el-table .cell) {
  font-size: 16px;
}
:deep(.el-table th > .cell) {
  font-size: 18px;
}
</style>
